package com.atguigu.edu.controller;


import com.atguigu.base.ExceptionHnadler.CustomException;
import com.atguigu.edu.entity.Comment;
import com.atguigu.edu.service.CommentService;
import com.atguigu.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author ld
 * @since 2022-08-24
 */
@RestController
@RequestMapping("/eduservice/comment")
//@CrossOrigin
@Api(description = "课程评论管理",value = "课程评论管理")
public class CommentController {
    @Resource
    private CommentService commentService;
    //分页展示评论信息
    @GetMapping("/pageInfo/{courseId}/{page}/{limit}")
    public R pageInfo(@PathVariable("courseId")String courseId,
                      @PathVariable("page")long page,
                      @PathVariable("limit")long limit
                      ){
        Page<Comment> pageInfo=new Page<>(page,limit);
        LambdaQueryWrapper<Comment> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getCourseId,courseId)
                 .orderByDesc(Comment::getGmtCreate);
        Map<String,Object> map=commentService.pageInfo(pageInfo, queryWrapper);
        return R.ok().data(map);
    }
    //添加课程评论
    @PostMapping("/addInfo")
    public R addInfo(@RequestBody Comment comment){
        boolean flag=commentService.save(comment);
        if(flag){
            return R.ok();
        }else {
            throw new CustomException(20001,"添加评论失败");
        }

    }
}

